/////////////////////////////////////////////////////////////////////////////
// Name:        wixtin.cpp
// Purpose:     
// Author:      Stephen Whittle
// Modified by: 
// Created:     07/06/2009 12:13:19
// RCS-ID:      
// Copyright:   
// Licence:     
/////////////////////////////////////////////////////////////////////////////

// For compilers that support precompilation, includes "wx/wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
#pragma hdrstop
#endif

#ifndef WX_PRECOMP
#include "wx/wx.h"
#endif

////@begin includes
#include "wxauipanel.h"
#include "wxmudcommandline.h"
////@end includes
#include "globals.h"
#include "wx/stdpaths.h"
#include "wx/filename.h"
#include "wixtin.h"
#include "wxmudtextctrl.h"
#include "libtelnet.h"
//#include "wxlua/include/wxlua.h"
//#include "lua.h"
//#include "scripting.h"
#include "wx/stdpaths.h"
#include "wx/filename.h"
#include <wx/arrstr.h>
////@begin XPM images
#include "images/xconsole.xpm"
////@end XPM images

#include "statusbarmetre.h"
enum commandTypes{
dummy,
dummy2




};



/*
 * wixtin type definition
 */

IMPLEMENT_CLASS( wixtin, wxFrame )


/*
 * wixtin event table definition
 */

BEGIN_EVENT_TABLE( wixtin, wxFrame )

////@begin wixtin event table entries
    EVT_CHAR( wixtin::OnChar )

    EVT_MENU( ID_MENUITEM1, wixtin::OnExitClick )

    EVT_MENU( ID_MENUITEM3, wixtin::OnMenuMainTabClick )

    EVT_MENU( ID_MENUITEM4, wixtin::OnMenuSubTabClick )

    EVT_MENU( ID_MENUITEM5, wixtin::OnMenuFloatClick )

    EVT_MENU( ID_MENUITEM8, wixtin::OnSaveDefaultConfiguration )

////@end wixtin event table entries

END_EVENT_TABLE()


/*
 * wixtin constructors
 */

wixtin::wixtin()
{
    Init();
}

wixtin::wixtin( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
    Init();
    Create( parent, id, caption, pos, size, style );
}


/*
 * wixtin creator
 */

bool wixtin::Create( wxWindow* parent, wxWindowID id, const wxString& caption, const wxPoint& pos, const wxSize& size, long style )
{
wxString currentPath = (wxStandardPaths::Get()).GetExecutablePath();
wxFileName currentDir = wxFileName(currentPath);
wxString path = currentDir.GetPath(wxPATH_GET_VOLUME | wxPATH_GET_SEPARATOR) + wxT("wixtin.cfg");
pConfig = new wxFileConfig(wxEmptyString, wxEmptyString, path, wxEmptyString, wxCONFIG_USE_LOCAL_FILE);
////@begin wixtin creation
    wxFrame::Create( parent, id, caption, pos, size, style );

    CreateControls();
    SetIcon(GetIconResource(wxT("images/xconsole.xpm")));
    Centre();
////@end wixtin creation
commandLine = m_InputBar;
    return true;
}


/*
 * wixtin destructor
 */

wixtin::~wixtin()
{
////@begin wixtin destruction
////@end wixtin destruction
}


/*
 * Member initialisation
 */

void wixtin::Init()
{
////@begin wixtin member initialisation
    m_AuiPanel = NULL;
    m_InputBar = NULL;
    m_Status = NULL;
////@end wixtin member initialisation
}


/*
 * Control creation for wixtin
 */

void wixtin::CreateControls()
{    
////@begin wixtin content construction
    wixtin* itemFrame1 = this;

    wxMenuBar* menuBar = new wxMenuBar;
    wxMenu* itemMenu3 = new wxMenu;
    itemMenu3->Append(ID_MENUITEM1, _("Exit"), wxEmptyString, wxITEM_NORMAL);
    menuBar->Append(itemMenu3, _("&File"));
    wxMenu* itemMenu5 = new wxMenu;
    itemMenu5->Append(ID_MENUITEM3, _("New Main Tab"), wxEmptyString, wxITEM_NORMAL);
    itemMenu5->Append(ID_MENUITEM4, _("New Sub Tab"), wxEmptyString, wxITEM_NORMAL);
    itemMenu5->Append(ID_MENUITEM5, _("New Floating Window"), wxEmptyString, wxITEM_NORMAL);
    itemMenu5->Append(ID_MENUITEM8, _("Save Default Window Configuration"), wxEmptyString, wxITEM_NORMAL);
    itemMenu5->Enable(ID_MENUITEM8, false);
    itemMenu5->Append(ID_MENUITEM6, _("Import Window Configuration"), wxEmptyString, wxITEM_NORMAL);
    itemMenu5->Enable(ID_MENUITEM6, false);
    itemMenu5->Append(ID_MENUITEM7, _("Export Window Configuration"), wxEmptyString, wxITEM_NORMAL);
    itemMenu5->Enable(ID_MENUITEM7, false);
    menuBar->Append(itemMenu5, _("Windows"));
    wxMenu* itemMenu12 = new wxMenu;
    itemMenu12->Append(ID_MENUITEM2, _("About"), wxEmptyString, wxITEM_NORMAL);
    itemMenu12->Enable(ID_MENUITEM2, false);
    menuBar->Append(itemMenu12, _("Help"));
    itemFrame1->SetMenuBar(menuBar);

    wxFlexGridSizer* itemFlexGridSizer14 = new wxFlexGridSizer(0, 1, 0, 0);
    itemFlexGridSizer14->AddGrowableRow(0);
    itemFlexGridSizer14->AddGrowableCol(0);
    itemFrame1->SetSizer(itemFlexGridSizer14);

    m_AuiPanel = new wxAuiPanel( itemFrame1, ID_PANEL, wxDefaultPosition, wxDefaultSize, wxSIMPLE_BORDER|wxTAB_TRAVERSAL );
    itemFlexGridSizer14->Add(m_AuiPanel, 0, wxGROW|wxGROW|wxALL, 0);

    m_InputBar = new wxMudCommandLine( itemFrame1, ID_CMDBARMAINCTRL, wxEmptyString, wxDefaultPosition, wxSize(-1, 25), wxTE_PROCESS_ENTER|wxSIMPLE_BORDER );
    m_InputBar->SetFont(wxFont(10, wxSWISS, wxNORMAL, wxNORMAL, false, wxT("")));
    itemFlexGridSizer14->Add(m_InputBar, 0, wxGROW|wxALIGN_CENTER_VERTICAL|wxBOTTOM, 0);

    m_Status = new wxStatusBar( itemFrame1, ID_STATUSBAR, wxST_SIZEGRIP|wxNO_BORDER );
    m_Status->SetFieldsCount(4);
    int m_StatusWidths[4];
    m_StatusWidths[0] = 200;
    m_StatusWidths[1] = 200;
    m_StatusWidths[2] = 200;
    m_StatusWidths[3] = -1;
    m_Status->SetStatusWidths(4, m_StatusWidths);
    itemFlexGridSizer14->Add(m_Status, 0, wxGROW|wxGROW|wxALL|wxFIXED_MINSIZE, 0);

////@end wixtin content construction
	(m_AuiPanel->GetAuiManager()).LoadPerspective(pConfig->Read(wxT("/Windows/Perspective")));
//eventually make this conditional
//m_GP = new statusBarMetre( m_Status, wxID_ANY, wxDefaultPosition, wxSize(200, 20), wxSIMPLE_BORDER );
//m_HP = new statusBarMetre( m_Status, wxID_ANY, wxDefaultPosition, wxSize(200, 20), wxSIMPLE_BORDER );
    this->Connect(wxID_ANY,wxEVT_SIZE,wxSizeEventHandler(wixtin::OnStatusbarResize));

    }


/*
 * Should we show tooltips?
 */

bool wixtin::ShowToolTips()
{
    return true;
}

/*
 * Get bitmap resources
 */

wxBitmap wixtin::GetBitmapResource( const wxString& name )
{
    // Bitmap retrieval
////@begin wixtin bitmap retrieval
    wxUnusedVar(name);
    return wxNullBitmap;
////@end wixtin bitmap retrieval
}

/*
 * Get icon resources
 */

wxIcon wixtin::GetIconResource( const wxString& name )
{
    // Icon retrieval
////@begin wixtin icon retrieval
    wxUnusedVar(name);
    if (name == _T("images/xconsole.xpm"))
    {
        wxIcon icon(xconsole_xpm);
        return icon;
    }
    return wxNullIcon;
////@end wixtin icon retrieval
} 



/*
 * wxEVT_COMMAND_TEXT_ENTER event handler for ID_TEXTCTRL
 */
/*
void wixtin::OnTextctrlEnter( wxCommandEvent& event )
{
//need to check if we are connected before doing this
    if (mudConnected){
        //check for string separator and for speedwalk too
        wxString input = m_InputBar->GetValue() + wxT("\r\n");
        telnet_send(&myTelnet_t,input.mb_str(),input.Length() ); 
        if ((m_InputBar->GetValue() != wxEmptyString)&&(m_InputBar->GetValue() != commandHistory[commandHistory.GetCount() -1])){
            commandHistory.Add(m_InputBar->GetValue());
        }
        bool doEcho;
        pConfig->Read(wxT("/Terminal/LocalEcho"), &doEcho, true); 
        if (doEcho){
            displayOutput(m_InputBar->GetValue() + wxT("\n"));
        }
        commandHistoryIndex = commandHistory.GetCount();
        m_InputBar->Clear();
        //event.Skip();
    }
}
*/

/*
 * wxEVT_COMMAND_MENU_SELECTED event handler for ID_MENUITEM1
 */

void wixtin::OnExitClick( wxCommandEvent& event )
{
////@begin wxEVT_COMMAND_MENU_SELECTED event handler for ID_MENUITEM1 in wixtin.
    // Before editing this code, remove the block markers.
    Destroy();
////@end wxEVT_COMMAND_MENU_SELECTED event handler for ID_MENUITEM1 in wixtin. 
}




/*
 * wxEVT_COMMAND_MENU_SELECTED event handler for ID_MENUITEM3
 */

void wixtin::OnMenuMainTabClick( wxCommandEvent& event )
{
m_AuiPanel->OnAddTopClick(event);
}


/*
 * wxEVT_COMMAND_MENU_SELECTED event handler for ID_MENUITEM4
 */

void wixtin::OnMenuSubTabClick( wxCommandEvent& event )
{
m_AuiPanel->OnAddBottomClick(  event );

}


/*
 * wxEVT_COMMAND_MENU_SELECTED event handler for ID_MENUITEM5
 */

void wixtin::OnMenuFloatClick( wxCommandEvent& event )
{
m_AuiPanel->OnNewFloatWindowClick( event );
}


/*
 * wxEVT_COMMAND_MENU_SELECTED event handler for ID_MENUITEM8
 */

void wixtin::OnSaveDefaultConfiguration( wxCommandEvent& event )
{

//mgr->SavePerspective();
pConfig->Write(wxT("/Windows/Perspective"), (m_AuiPanel->GetAuiManager()).SavePerspective());
pConfig->Flush();
}


//pop/push for command bar
//two arrays or something 




void wixtin::OnStatusbarResize(wxSizeEvent& event ){
    //wxRect rect;
    //m_Status->GetFieldRect(1, rect);


    //m_HP->SetSize(rect);

    //m_Status->GetFieldRect(2, rect);
    
    //m_GP->SetSize(rect);

event.Skip();
}




/*
 * wxEVT_KEY_DOWN event handler for ID_TEXTCTRL
 */
/*
void wixtin::OnInputKeyDown( wxKeyEvent& event )
{
    switch(event.GetUnicodeKey()){
        case 38: //UP
                wxLogDebug(wxT("%d -- T"),commandHistoryIndex);

            if (commandHistoryIndex > 0){
                if (commandHistoryIndex >1){
                    commandHistoryIndex--;
                }
                commandLine->ChangeValue(commandHistory[commandHistoryIndex]);
            }
            commandLine->SetInsertionPoint(commandLine->GetLastPosition());
            commandLine->SetFocus();
            break;
        case 40: //DOWN
                wxLogDebug(wxT("%d ++ %d T"),commandHistoryIndex,commandHistory.GetCount());
            if (commandHistoryIndex < commandHistory.GetCount()){
                
                if (commandHistoryIndex >= 0){
//                if commandHistoryIndex < commandHistory.GetCount()
                commandHistoryIndex++;
                wxLogDebug(wxT("%d ++ %d after increment"),commandHistoryIndex,commandHistory.GetCount());
                    if (commandHistoryIndex < commandHistory.GetCount()){
                        commandLine->ChangeValue(commandHistory[commandHistoryIndex]);
                    }else{
                        commandLine->Clear();                    
                    }
                }
            }else{
                commandLine->Clear();
            }
            commandLine->SetInsertionPointEnd();
            commandLine->SetFocus();
            break;
        default:
            event.Skip();
    }
}
*/
bool wixtin::ParseTinCommand(wxString commandLine){
wxString command;
wxString hash = wxT("#");
if (commandLine.StartsWith(wxT("#"),&command)){
    
    
    return true;
}else{
    return false;
}

}


/*
 * wxEVT_CHAR event handler for ID_WIXTIN
 */

void wixtin::OnChar( wxKeyEvent& event )
{
//commandLine->SetFocus();
//commandLine->AddPendingEvent(event);
}

